import { Equipment } from "@/types/equipment"
import { api } from "@/api/request"

/**
 * sysEquipment 相关的接口封装
 * 1.增加设备,createEquipment(newEquipment : Equipment)对象添加
 * 2.删除数据,deleteEquipment(id : number) 根据id逻辑删除一条数据
 * 3.修改数据,updateEquipment(updateEquipment : Equipment) 修改一条对象数据
 * 4.查询数据,readEquipment(sys_user_id:number) 根据user_id查询,返回非嵌套数组对象,用于user页面显示数量
 * 5.查询数据,getEquipmentById(id : number)根据表内id查询一条数据,用于修改数据前前端展示数据
 * 6.查询数据,getEquipmentByUserId(sys_user_id : number)根据user_id查询,返回3层嵌套数组对象
 */

/**
 * 增加设备
 * sys_equipment 管理页面调用
 * POST方法,发送对象数据
 * @param {Equipment} newEquipment 
 * @return 
 */
export async function createEquipment(newEquipment : Equipment) {
	let res = await api<string>({
		method: 'POST',
		url: '/equipment/save',
		data: newEquipment
	})
	if (res.code == 200) {
		return res.data
	}
}

/**
 * 删除设备数据,根据表内id逻辑删除
 * @param {number} id 需要的删除的数据的id
 * @return 
 */
export async function deleteEquipment(id : number) {
	let res = await api<string>({
		method: 'DELETE',
		url: '/equipment/deleteById/' + id,
	})
	if (res.code == 200) {
		return res.data
	}
}

/**
 * 修改设备数据,只可修改equipmentName和information
 * @param {Equipment} updateEquipment 修改的equipment对象数据
 * @return 返回修改的结果
 */
export async function updateEquipment(updateEquipment : Equipment) {
	let res = await api<string>({
		method: 'POST',
		url: '/equipment/updateById',
		data: updateEquipment
	})
	if (res.code == 200) {
		return res.data
	}
}

/**
 * 查询设备,根据 user_id 查询,后端完成多表查询
 * 1.用user_id 查询family_ids
 * 2.用family_ids 查询room_ids
 * 3.用room_ids 查询 sys_equipment 表,返回 equipment 数据
 * @return 返回3层嵌套的对象数组
 */
export async function readEquipmentByOwnerId(sys_user_id : number) {
	let res = await api<Equipment[]>({
		method: 'GET',
		url: '/equipment/listFamilyRoomEquipmentByOwnerId',
		data: {
			'sysUserId': sys_user_id                // 从Storage获得user_id
		}
	})
	if (res.code === 200) {
		return res.data                           // 数据返回成功
	}
}

/**
 * 查询设备,根据 user_id 查询,后端完成多表查询
 * 1.用user_id 查询family_ids
 * 2.用family_ids 查询room_ids
 * 3.用room_ids 查询 sys_equipment 表,返回 equipment 数据
 * @return 返回3层嵌套的对象数组
 */
export function readEquipmentByUserId(sys_user_id : number) {
	return api<Equipment[]>({
		method: 'GET',
		url: '/equipment/listFamilyRoomEquipmentByUserId',
		data: {
			'sysUserId': sys_user_id                // 从Storage获得user_id
		}
	})
}

/**
 * 查询设备,根据 sys_equipment 表的id查询
 * 单表主键查询
 * @param {number} id 本表的主键id
 * @return 
 */
export async function getEquipmentById(id : number) {
	let res = await api<Equipment>({
		method: 'GET',
		url: '/equipment/listById/' + id,
	})
	if (res.code == 200) {
		return res.data
	}
}

/**
 * 查询设备,根据 sys_equipment 表的id查询
 * @param {number} sys_user_id 用户id
 * @return 
 */
export async function getEquipmentByUserId(sys_user_id : number) {
	let res = await api<Equipment[]>({
		method: 'GET',
		url: '/equipment/listByUserId',
		data: {
			'sysUserId': sys_user_id
		}
	})
	if (res.code == 200) {
		return res.data
	}
}